package com.example.myxiala;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

public class MainActivity extends Activity {

	private PopupWindow popwind;
	private ImageView mImageView;
	private EditText mEditText;
	private ListView mListView;
	private List<String> datas;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		datas = new ArrayList<String>();
		
		for (int i = 0; i < 20; i++) {
			datas.add("1000000000"+i);
		}
		initView();
	
		
	}
	/**
	 * 
	 */
	private void initView() {
		mImageView = (ImageView)findViewById(R.id.down_arrow);
		mEditText =(EditText)findViewById(R.id.input);
		initListView();
		mImageView.setOnClickListener(new MyClickListener());
	}
	class MyClickListener implements OnClickListener{

		
		@Override
		public void onClick(View v) {
			popwind = new PopupWindow(MainActivity.this);
			
			popwind.setWidth(mEditText.getWidth());
			popwind.setHeight(200);
			
			popwind.setContentView(mListView);
			popwind.setOutsideTouchable(true);
			popwind.showAsDropDown(mEditText,0,0);
		}
		
	}
	private void initListView(){
		mListView = new ListView(this);
		mListView.setBackgroundResource(R.drawable.listview_background);
		mListView.setDivider(null);
		
		MyAdapter adapter = new MyAdapter();
		mListView.setAdapter(adapter);
		
	}
	class MyAdapter extends BaseAdapter{
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return datas.size();
	}

	
	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	
	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		
		View v = View.inflate(getApplicationContext(), R.layout.list_item, null);
		ImageView mImageViewDate = (ImageView)v.findViewById(R.id.delete);
		final TextView mTextViewList = (TextView)v.findViewById(R.id.tv_list_item);
		
		mTextViewList.setText(datas.get(position));
		mImageViewDate.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				datas.remove(position);
				notifyDataSetChanged();
			}
		});
		mTextViewList.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				mTextViewList.setText(datas.get(position));
				popwind.dismiss();
			}
		});
		
		return v;
	}
	
	}
	
}
